//
//  SetPassWordView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/4.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit
let TextFieldTag = 1000
class SetPassWordView: BaseView, UITableViewDataSource, UITableViewDelegate, TimerDelegate {
    lazy var tableView:UITableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.plain)
    var viewModel:SetPassWordViewModel?
    var passWordText:BaseTextField?
    var secPassWordText:BaseTextField?
    var yzText:BaseTextField?
    var countryBtn:CustomBtn?
    var timerBtn:TimerBtn?
    lazy var selectBtn:UIButton = {
        let btn =  CommonBtn.creatSelectBtnWithTitle(title: kText("确认"), y: 36, view: self, sel:  #selector(selectBtnAction))
        return btn
    }()
    lazy var footView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 100))
        return view
    }()
    
    override func setUpUI() {
        self.viewModel = dataSource as? SetPassWordViewModel
        
        self.addSubview(tableView)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.backgroundColor = UIColor.clear
        tableView.delegate = self
        tableView.dataSource = self
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.top.equalToSuperview()
        }
        
        tableView.tableFooterView = footView
        footView.addSubview(selectBtn)
        selectBtn.isEnabled = false
        CommonBtn.createImageWithColor(color: kDisableColor, btn: selectBtn, state: UIControlState.normal)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = viewModel?.cellfactory.cellIdentiferAtIndexPath(indexPath)
        let item = viewModel?.cellfactory.itemAtIndexPath(indexPath)
        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier!) as? BaseTableViewCell
        if cell == nil {
            switch item!.type! {
            case .cyPassWord:
                cell = PassWordWriteCell(style: .default, reuseIdentifier:cellIdentifier)
                cell?.selectionStyle = .none
                break
            case .cyItemTimer:
                cell = TimerTableCell(style: .default, reuseIdentifier:cellIdentifier)
                cell?.selectionStyle = .none
                break
                
            case .cyItemNumber:
                cell = NumberWriteCell(style: .default, reuseIdentifier:cellIdentifier)
                cell?.selectionStyle = .none
                break
            default : break
            }
            cell?.tag = indexPath.row
        }
        
        switch item!.type! {
        case .cyPassWord:
            let cell = (cell as! PassWordWriteCell)
            cell.passWordTextField.length = 20
            cell.passWordTextField.textDelegate = self
            if indexPath.row == 0 {
            passWordText = cell.passWordTextField
            }else if indexPath.row == 1 {
                secPassWordText = cell.passWordTextField
            }
            cell.setIteam(item!)
            break
        case .cyItemTimer:
            let cell = (cell as! TimerTableCell)
            timerBtn = cell.timerBtn
            cell.numTextField.length = 6
            cell.numTextField.textDelegate = self
            yzText = cell.numTextField
            cell.delegate = self
            cell.setIteam(item!)
            break
            
        case .cyItemNumber:
            let cell = (cell as! NumberWriteCell)
            cell.numTextField.length = 6
            cell.numTextField.textDelegate = self
            if indexPath.row == 0 {
                passWordText = cell.numTextField
                cell.numTextField.isSecureTextEntry = true
            }else if indexPath.row == 1 {
                secPassWordText = cell.numTextField
                cell.numTextField.isSecureTextEntry = true
            }else {
                yzText = cell.numTextField
                cell.numTextField.isSecureTextEntry = false
            }
            
            cell.setIteam(item!)
            break
            
            default : break
        }
        
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view:UIView = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 10))
        return view
    }
    
    
    @objc func selectBtnAction() -> Void {
        self.endEditing(true)
        if (viewModel?.isLoadBool)! {
            if !(viewModel?.passWord.judgePassWordLegal())! {
                CYMBShow.showMessage(kText("请输入8-20位数字字母组合"))
                return
            }
        }else {
            if (viewModel?.passWord.ch_length)! < 6 {
                CYMBShow.showMessage(kText("请输入6位支付密码"))
                return
            }
        }
        if viewModel?.passWord != viewModel?.newPassWord {
            CYMBShow.showMessage(kText("原密码与新密码不一致"))
            return
        }
        if (viewModel?.codeText.ch_length)! < 6 {
            CYMBShow.showMessage(kText("请输入6位数验证码"))
            return
        }
        
        if (viewModel?.isLoadBool)! {
            
           viewModel?.putPasswordInformation()
        }else {
            viewModel?.putCapital()
        }
    }
    
    func startTimer(tag: Int,btn: TimerBtn) {
        self.endEditing(true)
        if AppSecurityInfo.default.model.isMobileBind {
        viewModel?.sendCode(btn:btn)
        }else {
           viewModel?.sendEmailCode(btn:btn)
        }
    }
    
    func changeStates()  {
        if viewModel?.passWord.ch_length != 0 && viewModel?.newPassWord.ch_length != 0
            && viewModel?.codeText.ch_length != 0 {
            selectBtn.isEnabled = true
            CommonBtn.createImageWithColor(color:kThemeColor, btn: selectBtn, state: UIControlState.normal)
        }else {
            selectBtn.isEnabled = false
            CommonBtn.createImageWithColor(color: kDisableColor, btn: selectBtn, state: UIControlState.normal)
        }
        
    }
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}

extension SetPassWordView:BaseTextFieldDelegate {
    func textFieldDidChange(textField: BaseTextField) {
        if textField.isEqual(passWordText) {
            viewModel?.passWord = textField.text!
        }else if textField.isEqual(secPassWordText) {
            viewModel?.newPassWord = textField.text!
        }else {
            viewModel?.codeText = textField.text!
        }
        self.changeStates()
    }
    
    
    
    
}
